/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package javadrone.util;

//~--- non-JDK imports --------------------------------------------------------

import javadrone.server.ImageConsumer;

//~--- JDK imports ------------------------------------------------------------

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Nikzzay
 */
public class RapidImageDisplayer extends JPanel {
    private ImageConsumer iC;
    private Thread        imageDisplayer;
    private JLabel        jl;
    private boolean       stopFlag;

    public RapidImageDisplayer(final ImageConsumer iC) {
        jl = new JLabel();
        jl.setBounds(0, 0, 500, 500);
        setLayout(null);
        add(jl);
        this.iC = iC;
    }

    public void start() {
        imageDisplayer = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!stopFlag) {
                    try {
                        jl.setIcon(new ImageIcon(iC.take()));
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }

                    revalidate();
                    repaint();
                }
            }
        });
        imageDisplayer.start();
    }

    public void stop() {
        stopFlag = true;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
